package br.com.acs.CameraIP.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import br.com.acs.CameraIP.dao.CameraDao;
import br.com.acs.CameraIP.dao.CameraDaoImp;
import br.com.acs.CameraIP.model.Camera;


@FacesConverter(forClass=Camera.class, value="cameraConverter")
public class CameraConverter implements Converter {

	@Override
	public Object getAsObject(FacesContext context, UIComponent component,
			String value) {
		try {
	  		   if(value != null && value.trim().length() > 0){
				  CameraDao dao = new CameraDaoImp();
				  Camera camera = dao.getCamera(Long.parseLong(value));
				  return camera;
			  }
			  return null;
		   } catch (Exception e ){
			  return null;
	       }
	}

	@Override
	public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
		try {
	  		   if(arg2 != null){
				  CameraDao dao = new CameraDaoImp();
				  Camera camera = dao.getCamera((Long) arg2);
			      return camera.getId().toString();
			  }
			  return null;
		   } catch (Exception e ){
			  return null;
	       }
	}
	
	

}
